package com.gamejelly.srv;

import com.gamejelly.proxy.server.RpcServerHandler;
import com.gamejelly.proxy.server.ServerChannelInitializer;
import com.gamejelly.proxy.server.ServiceProvider;
import com.gamejelly.proxy.server.ServiceStarter;
import io.netty.channel.nio.NioEventLoopGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Main {
    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    @Bean
    ServiceProvider provider() {
        return new ServiceProvider();
    }

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(Main.class, args);
        NioEventLoopGroup loopGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors());

        //---------------装配和初始化-------------------
        ServiceProvider provider = context.getBean(ServiceProvider.class);
        ServerChannelInitializer initializer = new ServerChannelInitializer();
        RpcServerHandler handler = new RpcServerHandler();
        handler.setServiceProvider(provider);
        initializer.setRpcServerHandler(handler);

        ServiceStarter starter = new ServiceStarter(9321);
        starter.setWorkerGroup(loopGroup);
        starter.setServerChannelInitializer(initializer);
        starter.start();

        ServiceStarter starter1 = new ServiceStarter(9322);
        starter1.setWorkerGroup(loopGroup);
        starter1.setServerChannelInitializer(initializer);
        starter1.start();


    }
}
